<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:6}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.sszym">
                 <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:12}" style="text-align:center">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" @click="handleAdd">添加</a-button>
            <a-button type="primary" @click="handleExport">导出</a-button>
            <a-button type="primary" @click="handleBatchDelete">批量删除</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
    <a-card class="card" :bordered="false">
   <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :data="loadData"
      :pagination="{ pageSize: 10 }"
    >
    <template slot="action" slot-scope="text,record">
        <a @click="handleDelete(record.id)">删除</a>
      </template>
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </s-table>

      <!-- 审核 -->
      <people-add :peopleAddVisible="peopleAddVisible" @close="handleCloseAdd" @success="handleSubmitAdd" ></people-add>

    </a-card>
</div>
</template>

<script>
// import moment from 'moment'
import PeopleAdd from './peopleAdd'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  components: {
    STable,
    PeopleAdd
  },
  data () {
    return {
      peopleAddVisible: false,
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        sszym: '',
        ssyxm:''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '院系',
          dataIndex: 'yx'
        },
        {
          title: '专业',
          dataIndex: 'zy'
        },
        {
          title: '教授人数',
          dataIndex: 'jsrs'
        },
        {
          title: '教授人数',
          dataIndex: 'jsrs1'
        },
        {
          title: '非教授人数',
          dataIndex: 'fjsrs'
        },
        {
          title: '讲师人数',
          dataIndex: 'jiasrs'
        },
        {
          title: '其他人数',
          dataIndex: 'qtrs'
        },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],
      selectedRowKeys:[],
      selectedRows:[],
      // custom table alert & rowSelection
      options: {
        alert: { show: false },
        rowSelection: {
          onChange: (selectedRowKeys, selectedRows) => {
            console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          },
          onSelect: (record, selected, selectedRows) => {
            console.log(record, selected, selectedRows)
          },
          onSelectAll: (selected, selectedRows, changeRows) => {
            console.log(selected, selectedRows, changeRows)
          }
        }
      },
      selectMenu: {
        yx: [] ,// 院系
        zy:[]
      },
      recruitEditData: {},
         // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        // console.log('loadData.parameter', parameter)
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.recruitstat.reExamPersonManageList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
      // 多选
    onSelectChange (selectedRowKeys,selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 导出
    handleExport () {
      console.log('导出')
      this.$export('admissions/fsgzrys/export', this.queryParam)
    },
    // 添加
    handleAdd (data) {
      this.peopleAddVisible = true
    },
    // 批量删除
    handleBatchDelete () {
       console.log('批量删除')
        var deleteId = ''
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的数据')
        return
      }
      if(this.selectedRows && this.selectedRows.length){
        deleteId = this.selectedRows.map(v => {
          return v.id
        }).toString()
      }
      this.deleteData(deleteId)
    },
    // 添加结束
    handleSubmitAdd (d) {
      console.log(d)
      this.peopleAddVisible = false
      this.$message.success('添加成功')
      this.$refs.table.refresh(true)
    },
    // 关闭添加
    handleCloseAdd () {
      this.peopleAddVisible = false
    },
    // 删除
    handleDelete (code) {
      console.log('删除')
      this.deleteData(code)
    },
    deleteData(code){
      this.$api.recruitstat.reExamPersonManageDelete(code).then( res => {
        this.$refs.table.loadData()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
